package com.ocp.socket.common.utils;

import org.apache.mina.util.Base64;

import java.security.Key;
import java.time.Duration;
import java.time.Instant;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class Des3 {

    public static void main(String[] args){
        try {
            Des3 des3 = new Des3();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public Des3() throws Exception {
        // 密钥
        byte[] key = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
        byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };

        //byte[] data="0154TEMP1000000000111331DATA20190405065932L01 17.4,L02 14.9,L03 20.7,L04 15.3,L05 21.0,L06 20.7,L07 22.0,L08 26.0,L09 25.2,L10 18.0,L11 24.1,L12 15.3,L13 19.4".getBytes("UTF-8");
        //byte[] data="0048MILK1000000000000003DATA20190309010600L01-0L02-1".getBytes("UTF-8");
        // byte[] data="0050AIRP1000000000000003DATA20190405081254L01011.52PKA".getBytes("UTF-8");
        //byte[] data="00380000USER000000000018DATA20190403154857".getBytes("UTF-8");
        byte[] data="0024NORM0000000000000000TIME".getBytes("UTF-8");

        Integer a =1;
        Integer b =1;
        System.out.println(a==b);
        System.out.println("ECB加密解密");
        byte[] str3 = des3EncodeECB(key,data );
        byte[] str4 = ees3DecodeECB(key, str3);
        System.out.println(new String(Base64.encodeBase64(str3), "UTF-8"));
        System.out.println(new String(str4, "UTF-8"));

        System.out.println("CBC加密解密");
        byte[] str5 = des3EncodeCBC(key, keyiv, data);
        byte[] str6 = des3DecodeCBC(key, keyiv, str5);
        System.out.println(Base64.encodeBase64(str5));
        System.out.println(new String(str6, "UTF-8"));


    }


    public static byte[] des3EncodeECB(byte[] key, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(data);

        return bOut;
    }


    public static byte[] ees3DecodeECB(byte[] key, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);


        Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, deskey);

        byte[] bOut = cipher.doFinal(data);

        return bOut;

    }


    public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] bOut = cipher.doFinal(data);

        return bOut;
    }


    public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);

        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);

        byte[] bOut = cipher.doFinal(data);

        return bOut;

    }
}
